/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mamayukero.problem.environment.component;

import java.awt.Point;
import java.util.ArrayList;
import mamayukero.debug.LogContainer;
import mamayukero.problem.environment.Environment;

public class Factory extends Warehouse{
    public int counter;
    public String workSchedule;
    public int[] production;
    public LogContainer log;
    
    public Factory() {
        location =  new Point(0,0);
        machines = new ArrayList<Machine>();
        workSchedule = "0000";
        log = new LogContainer("FACTORY");
    }

    public void initProduction() {
        production = new int[Environment.numberOfMachine];
        for (int i = 0; i < Environment.numberOfMachine; i++)
            production[i] = 0;
        log.append("Start a new job");
    }
    
    public void incCounter() {
        int isOwn = own(workSchedule.charAt(0)-48);
        if ((!workSchedule.isEmpty()) && (isOwn > -1) && (!machines.get(isOwn).isActive)) {
            for (Machine m:machines) {
                m.Deactivate();
            }
            machines.get(isOwn).Activate();
        }
        if (counter >= 60) {
            counter = 0;
            //harusnya produksi
                Character c = workSchedule.charAt(0);
                workSchedule = workSchedule.substring(1);
                production[c-49]+= Environment.typesOfMachine.get(c-49).productiv;
                log.append("Machine " + (c-48) + " produces " + Environment.typesOfMachine.get(c-49).productiv + " unit(s)");
                if (Environment.typesOfMachine.get(c-49).dependant != -1) {
                    production[Environment.typesOfMachine.get(c-49).dependant-1] -= Environment.typesOfMachine.get(c-49).inputAmount;
                    log.append("\tit requires " + Environment.typesOfMachine.get(c-49).inputAmount + " unit(s) of product " + Environment.typesOfMachine.get(c-49).dependant);
                }
                countMoney();
        } else if ((!workSchedule.isEmpty()) && (own(workSchedule.charAt(0)-48)) > -1)
            counter++;
    }
    
    public int countAssets() {
        int result = 0;
        if (production != null) {
            for (int i = 0; i < Environment.numberOfMachine; i++) {
                result += production[i]*Environment.typesOfProduct.get(i).price;
            }
        }
        return result;
    }
    
    public int countRent() {
        int result = 0;
        if (Environment.typesOfMachine != null) {
            for (Machine m : machines) {
                result += Environment.typesOfMachine.get(m.type-1).rentPrice;
            }
        }
        return result;
    }
    
    public void countMoney() {
    			Environment.currentMoney = Environment.startingMoney + countAssets() - countRent();
    }
    
    public double countProfit()
        {
            if (countAssets() > 0 || countRent() > 0)
                return (((double) (countAssets()-countRent())) / countRent())  * 100 ;
            else
                return 0;
        }

    @Override
    public String toString() {
            StringBuilder result = new StringBuilder("work schedule = [" + (workSchedule.isEmpty()? "" : workSchedule.charAt(0)) + "|" + counter + "] <- [" + 
                                                                                (workSchedule.isEmpty()? "" : workSchedule.substring(1)) + 
                                                                                "] \n[F] (" + location.x + "," + location.y + ")");
            for (Machine m : machines) {
                result.append(" [").append(m.type).append("|").append(production[m.type-1]).append("]");
            }
            result.append("\nResult = ").append(countAssets()).append(" - ").append(countRent()).append(" = ").append(countAssets() - countRent()).append(" (").append((((float) (countAssets() - countRent()))/Environment.startingMoney)*100).append("% Profit)");
            return result.toString();
    }
}
